'''
Author xzk
Create Time 2020.7.17上午
Change Time1 2020.7.17上午
Pull Time 2020.7.17晚上
Results 对数据库的插入，更新操作
'''

import pymysql
import datetime
from query import query_one_parking_space, query_carinpark_by_plate


# 停车信息录入（车位ID，车牌号，车型）
def input_enter_info(id, car_plate, car_kind):
    if query_one_parking_space(id) == True:
        conn = pymysql.connect(
            host="cdb-4o4cwcko.bj.tencentcdb.com",
            port=10083,
            db="car",
            user="root",
            password="njlj2666",
            charset="utf8"
        )
        cls = conn.cursor()
        sql = "update slots set Car_Plate=%s, Car_Kind=%s, IsFree=%s where ID=%s"
        try:
            cls.execute(sql, [car_plate, car_kind, 1, id])
            conn.commit()
        except:
            conn.rollback()
        finally:
            conn.close()
    else:
        raise Exception('%s车位已被占用。' % (id))


# 车辆离库信息录入（车牌号）
def input_leave_info(car_plate):
    conn = pymysql.connect(
        host="cdb-4o4cwcko.bj.tencentcdb.com",
        port=10083,
        db="car",
        user="root",
        password="njlj2666",
        charset="utf8"
    )
    cls = conn.cursor()
    sql = "update slots set Car_Plate=%s, Car_Kind=%s, IsFree=%s where Car_Plate=%s"
    try:
        cls.execute(sql, [None, None, 0, car_plate])
        conn.commit()
    except:
        conn.rollback()
    finally:
        conn.close()


# （入库）停车记录录入（车牌号）
def input_entry_record(car_plate):
    car_leave = query_carinpark_by_plate(car_plate)
    if car_leave is None:
        conn = pymysql.connect(
            host="cdb-4o4cwcko.bj.tencentcdb.com",
            port=10083,
            db="car",
            user="root",
            password="njlj2666",
            charset="utf8"
        )
        cls = conn.cursor()
        sql = "insert into parking_record values(null,%s,null,null)"
        try:
            cls.execute(sql, [car_plate])
            conn.commit()
        except:
            conn.rollback()
        finally:
            conn.close()
    else:
        raise Exception('%s已在车库中。' % (car_plate))


# （出库）停车记录完成（车牌号） 返回值：入库时间
def input_leave_record(car_plate):
    car_leave = query_carinpark_by_plate(car_plate)
    if car_leave is not None:
        conn = pymysql.connect(
            host="cdb-4o4cwcko.bj.tencentcdb.com",
            port=10083,
            db="car",
            user="root",
            password="njlj2666",
            charset="utf8"
        )
        cls = conn.cursor()
        sql = "update parking_record set leave_time=%s where id=%s"

        now_stamp = datetime.datetime.now()

        entry_stamp = car_leave[2]

        try:
            cls.execute(sql, [now_stamp, car_leave[0]])
            conn.commit()
            return entry_stamp
        except:
            conn.rollback()
        finally:
            conn.close()
    else:
        raise Exception('%s不在车库中。' % (car_plate))


if __name__ == '__main__':
    '''
    input_enter_info(7, '沪A12345', '迈巴赫')
    input_leave_info('沪A12345')
    '''
    print(input_leave_record('皖A88888'))
